<template>
  <div class="header">
    <!-- 折叠按钮 -->
    <div class="collapse-btn" @click="collapseChage">
      <i class="el-icon-menu"></i>
    </div>
    <div class="logo">户籍管理系统</div>
    <div class="header-right">
      <div class="header-user-con">
        <!-- 用户名下拉菜单 -->
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            当前账号：{{ name }}
            <template></template>
            <i class="el-icon-caret-bottom"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-if="role=='admin'" divided command="reset"
              >重置数据</el-dropdown-item
            >
            <el-dropdown-item divided command="loginout"
              >退出登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import bus from "../../utils/bus";
import { storage } from "../../utils/storage";
export default {
  data() {
    return {
      name: storage.getSession("name"),
      role: storage.getSession("role"),
      collapse: false,
      fullscreen: false
    };
  },
  methods: {
    // 用户名下拉菜单选择事件
    handleCommand(command) {
      if (command == "loginout") {
        this.logout();
      } else if (command == "reset") {
        this.dataReset();
      }
    },
    // 侧边栏折叠
    collapseChage() {
      this.collapse = !this.collapse;
      bus.$emit("collapse", this.collapse);
    },
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    dataReset() {
      this.$confirm("此操作初始化平台数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        setTimeout(() => {
          localStorage.clear();
          sessionStorage.clear();
          this.$router.push("/login");
          this.$message({
            type: "success",
            message: "初始化成功!"
          });
        }, 500);
      });
    }
  },
  mounted() {
    if (document.body.clientWidth < 1500) {
      this.collapseChage();
    }
  }
};
</script>

<style lang="scss" scoped>
.modify-dialog {
  .el-input {
    width: 200px;
  }
}
.header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  min-width: 1366px;
  height: 70px;
  color: #333;
  background-color: #fff;
  border-bottom: 1px solid #eee;
}
.collapse-btn {
  float: left;
  font-size: 22px;
  padding: 0 21px;
  cursor: pointer;
  line-height: 70px;
}
.header {
  .logo {
    float: left;
    width: 250px;
    line-height: 70px;
    font-weight: bold;
  }
}
.header-right {
  float: right;
  padding-right: 50px;
}
.header-user-con {
  display: flex;
  height: 70px;
  align-items: center;
}
.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
}
.btn-bell-badge {
  position: absolute;
  right: 0;
  top: -2px;
  width: 8px;
  height: 8px;
  border-radius: 4px;
  background: #f56c6c;
  color: #fff;
}
.btn-bell {
  .el-icon-bell {
    color: #fff;
  }
}
.user-name {
  margin-left: 10px;
}
.user-avator {
  margin-left: 20px;
}
.user-avator {
  img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
}
.el-dropdown-link {
  cursor: pointer;
}
.el-dropdown-menu__item {
  text-align: center;
}
</style>
